from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.

class User(AbstractUser):
    mobile = models.CharField(max_length=11,blank=True,default='')
    email_active = models.BooleanField(default='False',blank=False)

    def __str__(self):
        return self.username
class Category(models.Model):
    name = models.CharField(max_length=100,blank=False)

    def __str__(self):
        return self.name
class Product(models.Model):
    name = models.CharField(max_length=100,blank=False)
    price = models.FloatField(max_length=100,blank=False,default=0)
    default_image_url = models.CharField(max_length=100,blank=True,default='')
    category = models.ForeignKey(Category,on_delete=models.SET_NULL,null=True)
    sales = models.IntegerField(blank=False,default=0)
    create_time = models.DateTimeField(blank=False)
    skus_id = models.CharField(max_length=100,blank=False)
    quantity = models.IntegerField(blank=False,default=0)


    def get_comment_count(self):
        count = len(self.comment_set.all())
        return count

    def display_category(self):
        return self.category.name

    def display_create_time(self):
        return self.create_time.strftime('%Y-%m-%d %H:%M:%S')

    display_category.short_description = "category"

    def __str__(self):
        return self.name

class Comment(models.Model):
    product = models.ForeignKey(Product,on_delete=models.CASCADE,null=False)
    customer = models.ForeignKey(User,on_delete=models.SET_NULL,null=True)
    title = models.CharField(max_length=50,blank=False,default='')
    content = models.TextField(max_length=1000,blank=False,default='')
    stars=models.IntegerField(blank=False,default=5)
    create_time = models.DateTimeField(blank=False)
    anonymity = models.BooleanField(blank=False,default=False)

    def __str__(self):
        return self.title
class Cart(models.Model):
    customer = models.ForeignKey(User, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return "Cart for %s" % self.customer
class Item(models.Model):
    customer = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    product = models.ForeignKey(Product, on_delete=models.DO_NOTHING, null=False)
    count = models.IntegerField(blank=False,default=0)
    selected = models.BooleanField(blank=False,default=True)
    cart = models.ForeignKey(Cart,on_delete=models.CASCADE,null=False)
    order = models.ForeignKey('Order',on_delete=models.CASCADE,null=True)
    def __str__(self):
        return "%s * %d" % (self.product.name,self.count)

class Address(models.Model):
    customer = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    sender= models.CharField(max_length=100,blank=False,default='')
    send_address= models.CharField(max_length=100,blank=False,default='')
    reciever = models.CharField(max_length=100,blank=False,default='')
    recieve_address=models.CharField(max_length=100,blank=False,default='')

    def __str__(self):
        return self.recieve_address
class Express(models.Model):
    name =models.CharField(max_length=50,blank=False)

    def __str__(self):
        return self.name
class Pay(models.Model):
    name = models.CharField(max_length=50,blank=False)

    def __str__(self):
        return self.name

class Order(models.Model):
    customer = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    address = models.ForeignKey(Address,on_delete=models.CASCADE,null=True)
    express = models.ForeignKey(Express,on_delete=models.CASCADE,null=True)
    express_cost= models.FloatField(max_length=30,blank=False,default=0)
    total = models.FloatField(max_length=30,blank=False,default=0)
    pay_method = models.ForeignKey(Pay,on_delete=models.CASCADE,null=True)

    def __str__(self):
        return "order for %s" % self.customer

    # def get_total(self):
    #     sum=0
    #     items = self.item_set.all()
    #     for item in items:
    #         sum += item.product.price * item.count
    #     sum += self.express_cost
    #
    #     return sum


